<!-- 教师登录 -->

<template>
  <div class="wrapper">
    <div class="login-box">

      <div class="login-title">
        <b>教师登录</b>
      </div>

      <el-form status-icon :model="teacher" :rules="rules" ref="teacherFrom" style="width: 80%;margin: 0 auto">
        <el-form-item prop="username">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-s-custom"
                    v-model="teacher.username" placeholder="请输入用户名" clearable></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock"
                    v-model="teacher.password" placeholder="请输入密码" show-password clearable></el-input>
        </el-form-item>
      </el-form>

      <div style="margin-top: 30px;text-align: center">
        <el-button type="primary" size="medium" autocomplete="on" @click="TeaLogin" @keyup.enter="keyDown(e)">登录
        </el-button>

        <el-button type="warning" size="medium" autocomplete="off" style="margin-left: 30px"
                   @click="$router.push('/TeaRegister')">注册
        </el-button>
      </div>

      <div style="margin-top: 15px;text-align: right">
        <router-link to="/TeaForgotPassword" style="text-decoration: none;color: black">
          <span id="forgotPassword">忘了密码？</span>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "TeaLogin",
  data() {
    return {
      teacher: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1, max: 8, message: '用户名不能超过8个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 12, message: '密码不能超过12个字符', trigger: 'blur'}
        ],
      }
    }
  },
  mounted() {
    // 绑定监听事件
    window.addEventListener("keydown", this.keyDown);
  },
  destroyed() {
    // 销毁事件
    window.removeEventListener("keydown", this.keyDown, false);
  },
  methods: {
    // 登录
    TeaLogin() {
      this.$refs['teacherFrom'].validate((valid) => {
        if (valid) {
          this.request.post("/teacher/tLogin", this.teacher).then(res => {
            if (res.code === "code_200") {
              localStorage.setItem("Teacher", JSON.stringify(res.data))
              this.$router.push("/System_TeaBackend/Welcome")
              this.$message.success("登录成功！")
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },

    // 回车键登录
    keyDown(e) {
      // 回车则执行登录方法 enter键的ASCII是13
      if (e.keyCode === 13 || e.keyCode === 100) {
        // 登录方法
        this.TeaLogin();
      }
    }
  }
}
</script>

<style scoped>
.wrapper {
  height: 100vh;
  background-image: url("../assets/bj.jpg");
  overflow: hidden;
}

.login-box {
  margin: 200px auto;
  width: 350px;
  height: 300px;
  padding: 20px;
  border-radius: 10px;
  background: rgba(223, 219, 219, 0.2);
  box-shadow: 0 25px 35px rgba(0, 0, 0, 0.8);
}

.login-title {
  margin: 20px 0;
  text-align: center;
  font-size: 24px;
}

.login-title b {
  color: rgb(0, 0, 0);
  text-shadow: 2px 8px 6px rgba(124, 112, 112, 0.2), 0 -5px 35px rgb(255, 255, 255);
  background-image: linear-gradient(to right bottom, #0000ff, #ff0000); /* 线性渐变背景，方向向上 */
  -webkit-background-clip: text; /* 背景被裁剪成文字的前景色 */
  -webkit-text-fill-color: transparent; /* 文字填充颜色变透明 */
}

#forgotPassword:hover{
  color: #ca1028;
}
</style>